package in.iqing.control.adapter;

import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.Collection;
import java.util.HashSet;
import java.util.List;
import java.util.Set;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;
import in.iqing.app.R;
import in.iqing.model.bean.DirectoryItem;

/**
 * Created by ZuoShu on 15/7/9.
 */
public class DirectoryAdapter extends RecyclerView.Adapter<DirectoryAdapter.ViewHolder> {

    private static final int TYPE_VOLUME = 1;
    private static final int TYPE_CHAPTER = 2;

    private Set<Integer> downloadedChapters;
    private List<DirectoryItem> directoryItems;
    private OnChapterClickListener listener;

    private int lastReadChapterId;

    public interface OnChapterClickListener {
        void onChapterClick(DirectoryItem chapter);
    }

    public DirectoryAdapter() {
        downloadedChapters = new HashSet<>();
    }

    public void setLastReadChapterId(int lastReadChapterId) {
        this.lastReadChapterId = lastReadChapterId;
    }

    public void setListener(OnChapterClickListener listener) {
        this.listener = listener;
    }

    public void setDirectoryItems(List<DirectoryItem> directoryItems) {
        this.directoryItems = directoryItems;
    }

    public void addDownloadedChapter(int chapterId) {
        downloadedChapters.add(chapterId);
    }

    public void addDownloadedChapters(Collection<Integer> chapters) {
        downloadedChapters.addAll(chapters);
    }

    public void clearDownloadChapters(){
        downloadedChapters.clear();
    }

    @Override
    public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        switch (viewType) {
            case TYPE_VOLUME:
                return new VolumeViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.item_volume, parent, false));
            case TYPE_CHAPTER:
                return new ChapterViewHolder(LayoutInflater.from(parent.getContext()).inflate(R.layout.item_chapter, parent, false));
            default:
                return null;
        }
    }

    @Override
    public void onBindViewHolder(ViewHolder holder, int position) {
        DirectoryItem item = directoryItems.get(position);
        holder.item = item;
        if (holder instanceof VolumeViewHolder) {
            holder.title.setText(item.getTitle());
        } else if (holder instanceof ChapterViewHolder) {
            holder.title.setText(item.getTitle());
            ((ChapterViewHolder) holder).continueRead.setVisibility(
                    item.getChapter().getId() == lastReadChapterId ? View.VISIBLE : View.GONE
            );
            ((ChapterViewHolder) holder).finishImage.setVisibility(
                    downloadedChapters.contains(item.getChapter().getId()) ? View.VISIBLE : View.GONE
            );
        }
    }

    @Override
    public int getItemCount() {
        return directoryItems == null ? 0 : directoryItems.size();
    }

    @Override
    public int getItemViewType(int position) {
        DirectoryItem item = directoryItems.get(position);
        switch (item.getType()) {
            case VOLUME:
                return TYPE_VOLUME;
            case CHAPTER:
                return TYPE_CHAPTER;
            default:
                return TYPE_CHAPTER;
        }
    }

    class ViewHolder extends RecyclerView.ViewHolder {
        @Bind(R.id.title)
        TextView title;

        DirectoryItem item;

        public ViewHolder(View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }

    class VolumeViewHolder extends ViewHolder {

        public VolumeViewHolder(View itemView) {
            super(itemView);
        }
    }

    class ChapterViewHolder extends ViewHolder {

        @Bind(R.id.continue_read_text)
        TextView continueRead;

        @Bind(R.id.finish_image)
        ImageView finishImage;

        public ChapterViewHolder(View itemView) {
            super(itemView);
        }

        @OnClick(R.id.chapter_layout)
        public void onChapterClick(View v) {
            if (listener != null) {
                listener.onChapterClick(item);
            }
        }
    }

}
